//将课程的字符串转换成数组
function sectionsStr2List(sectionsStr) {
    //console.info(sectionsStr);
    let secTime = [];
    let sectionSplit = sectionsStr.split("-");
    let start = parseInt(sectionSplit[0]);
    let end = parseInt(sectionSplit[1]);
    for (let i = start; i <= end; i++) {
        let sec = {
            section: i
        }
        secTime.push(sec);
    }
    return secTime;
}

//在字符串中查找课程的名字  解决课程名字里面带特殊符号
function findClassNameInStr(classStr) {
    let className0 = classStr.split("周)");
    let isSpecialWeekTime = className0[0].includes("(单)") || className0[0].includes("(双)");
    let j = className0[0].length;
    let i=0;
    if (isSpecialWeekTime) {
        i = className0[0].lastIndexOf("(", j - 4);  //获取除了"(单)""(双)"以外最后一个"("的位置
    } else {
        i = className0[0].lastIndexOf("(");         //获取最后一个"("的位置
    }
    let className = className0[0].substr(0, i);     //截取课程名字
    return className;
}

//在字符串中查找上课周数 解决课程名字中带有奇奇怪怪符号引起的问题
//请把经过"周)"的字符串传给本函数 避免漏课
function findWeekStr(str) {
    let isSpecialWeekTime = str.includes("(单)") || str.includes("(双)");
    let i = 0;
    let j = str.length;
    if (isSpecialWeekTime) {
        i = str.lastIndexOf("(", j - 4);       //获取除了"(单)""(双)"以外最后一个"("的位置
    } else {
        i = str.lastIndexOf("(");       //获取最后一个"("的位置
    }
    let WeekStr = str.substr(++i, j - i);    //截取上课时间
    //console.info(WeekStr);
    return WeekStr;
}

//包含周数的字符串转化为{1,2,3,4,5,...,17,18}
//1~3
//1~15(单)
//4~14(双)
function weekStr2IntList(week) {
    //单双周
    //console.info(week);console.info(courseWeeks);
    let courseWeeks = [];
    if (week.search(',') != -1) {
        let weeks = week.split(",");
        for (let i = 0; i < weeks.length; i++) {
            if (weeks[i].search('单') != -1 || weeks[i].search('双') != -1) {
                let rangeStr = weeks[i].split("(");
                let range = rangeStr[0].split("~");

                let start = parseInt(range[0]);
                let end = parseInt(range[1]);
                for (let i = start; i <= end; i += 2) {//间隔一周
                    if (!courseWeeks.includes(i)) {
                        courseWeeks.push(i);
                    }
                }
            } else {
                //连续
                let range = weeks[i].split("~");
                if (range.length == 2) {//这个是只有一周的处理
                    let start = parseInt(range[0]);
                    let end = parseInt(range[1]);
                    for (let i = start; i <= end; i++) {
                        if (!courseWeeks.includes(i)) {
                            courseWeeks.push(i);
                        }
                    }
                } else {
                    let start = parseInt(weeks[i]);
                    if (!courseWeeks.includes(start)) {
                        courseWeeks.push(start);
                    }
                }

            }
        }
    } else if (week.search('单') != -1 || week.search('双') != -1) {
        let rangeStr = week.split("(");
        let range = rangeStr[0].split("~");

        let start = parseInt(range[0]);
        let end = parseInt(range[1]);
        for (let i = start; i <= end; i += 2) {//间隔一周
            if (!courseWeeks.includes(i)) {
                courseWeeks.push(i);
            }
        }
    } else {
        //连续
        let range = week.split("~");
        if (range.length == 2) {//这个是只有一周的处理
            let start = parseInt(range[0]);
            let end = parseInt(range[1]);
            for (let i = start; i <= end; i++) {
                if (!courseWeeks.includes(i)) {
                    courseWeeks.push(i);
                }
            }
        } else {
            let start = parseInt(week);
            if (!courseWeeks.includes(start)) {
                courseWeeks.push(start);
            }
        }

    }
    return courseWeeks;
}
//课程解析函数(单元格源数据,行.列)

//★计算机组成原理(1~3周) (1-2节) 禹洲0304* 贺冰琰(10~13周) (1-2节)  陆大0304  贺冰琰
//★计算机网络(4~14(双)周) (1-2节) 陆大0216 耿少峰
//★算法设计与分析(1~15(单)周) (5-6节) 陆大0208 廖江福
//★移动通信(3~11周) (1-2节) 美岭0205* 林子杰★天线理论与设计(3~12周) (3-4节) 克立0213 陈彭
//★海岸动力学(1~8周) (5-6节) 建发0222* 钟小菁★土力学与地基基础(12~14周) (5-6节) 集诚A0321 潘以恒
//★计算机辅助设计(1~9周) (9-11节) 集诚A0318 林煌斌(10~11周) (9-10节)  集诚A0318  林煌斌★计算机辅助设计(10周) (11-11节) 集诚A0318 林煌斌
//★大学英语(1)[听说](4~13周) (3-4节) 新师0220* 朱梅花★英语实践教学(1)(14~16周) (3-4节) 线上学习（课堂派平台） 朱梅花

//以★分割,以"周)",以最后一个"("分割后第0段字符串作为课程名
//以"周)"切割 有第二段就做特殊处理,  以"★"+课程名+"("分割   作为上课周数
//结合课程位置及"节)"分割来确定上课时间
//空格分隔的最后一位作为教师名字

var CourseResult = [];
function CourseParser(data, Row, Column) {
    //const weekReg = new RegExp(/(?<=\().+$/);     //课程名字里面带括号会出错 不能这样找
    try {
        if (data.search('★') != -1) {              //存在课程
            let multiClass = false;                 //多课程标志位
            let classSplit = data.split("★");      //切割课程字符串
            if (classSplit.length > 2) {
                multiClass = true;
                console.info("存在多个课程");
            }
            //处理每节课
            for (var i = 1; i < classSplit.length; i++) {   //星号切割之后 遍历
                //let multiClassTime = false;     //课程存在多个时间
                let courseDay = 0;                              //得出周几
                switch (Row) {                                  //判断是周几
                    case 1:
                    case 5:
                    case 9: courseDay = Column - 1;
                        break;
                    default: courseDay = Column;
                        break;
                }

                let sections = [];              //节数


                let courseName = findClassNameInStr(classSplit[i]);    //得出课程名字

                let weekStrSplit = classSplit[i].split("周)");         //切割周数
                let courseWeeks = [];//上课周数的变量
                //做特殊处理
                if (weekStrSplit.length == 2) {//一节课一个时间
                    courseWeeks = weekStr2IntList(findWeekStr(weekStrSplit[0]));//得出上课的周数
                    let sectionsStr = classSplit[i].split(" (");
                    sectionsStr = sectionsStr[1].split("节)");              //得出节数
                    sections = sectionsStr2List(sectionsStr[0]);

                    let spaceSplit = classSplit[i].split(" ");              //以空格进行切割
                    let teachername = spaceSplit[spaceSplit.length - 1];    //得出老师的名字
                    let classPos = "";
                    if (spaceSplit[spaceSplit.length - 2]) {
                        classPos = spaceSplit[spaceSplit.length - 2];
                    } else {
                        classPos = spaceSplit[spaceSplit.length - 3];
                    }
                    let course = {
                        name: courseName,
                        position: classPos,
                        teacher: teachername,
                        weeks: courseWeeks,
                        day: courseDay,
                        sections: sections,
                    }
                    console.info(course);
                    CourseResult.push(course)

                } else {//一节课多个时间
                    console.info("存在多个时间");
                    courseWeeks = weekStr2IntList(findWeekStr(weekStrSplit[0]));//得出第一段上课的周数

                    let sectionsStr = classSplit[i].split(" (");
                    sectionsStr = sectionsStr[1].split("节)");
                    sections = sectionsStr2List(sectionsStr[0]);       //得出第一段上课节数

                    let spaceSplit = classSplit[i].split(" ");         //以空格进行切割
                    let teachernamestr = spaceSplit[3].split("(");
                    let teachername = teachernamestr[0];               //得出第一段老师的名字

                    let classPos = spaceSplit[2];                      //得出第一段上课地点


                    let course = {
                        name: courseName,
                        position: classPos,
                        teacher: teachername,
                        weeks: courseWeeks,
                        day: courseDay,
                        sections: sections,
                    }
                    console.info(course);
                    CourseResult.push(course)

                    let matches = weekStrSplit[1].split("节");              //防止单双周字段的影响
                    courseWeeks = [];//上课周数的变量
                    courseWeeks = weekStr2IntList(findWeekStr(matches[1]));                                 //得出第二段上课的周数

                    sectionsStr = classSplit[i].split(" (");
                    sectionsStr = sectionsStr[2].split("节)");
                    sections = sectionsStr2List(sectionsStr[0]);                                            //得出第二段上课节数

                    teachername = spaceSplit[spaceSplit.length - 1];                                          //得出第二段老师的名字

                    classPos = spaceSplit[spaceSplit.length - 3];                                               //得出第二段上课地点

                    course = {
                        name: courseName,
                        position: classPos,
                        teacher: teachername,
                        weeks: courseWeeks,
                        day: courseDay,
                        sections: sections,
                    }
                    console.info(course);
                    CourseResult.push(course)
                }

            }
        }

    } catch (e) {
        console.info(e);
    }

}

function scheduleHtmlParser(html) {
    //除函数名外都可编辑
    //传入的参数为上一步函数获取到的html
    //可使用正则匹配
    //可使用解析dom匹配，工具内置了$，跟jquery使用方法一样，直接用就可以了，参考：https://juejin.im/post/5ea131f76fb9a03c8122d6b9

    //-------------表结构-------------
    //0     0 1 2 3 4 5
    //1     0 1 2 3 4 5 6 7 8
    //2     0
    //3     0 1 2 3 4 5 6 7
    //4     0
    //5     0 1 2 3 4 5 6 7 8
    //6     0
    //7     0 1 2 3 4 5 6 7
    //8     0
    //9     0 1 2 3 4 5 6 7 8
    //10    0
    //11    0
    const $ = cheerio.load(html);
    $(html).find('tbody').find('tr').each(function (Row) {          //遍历表格的行
        $(this).find('td').each(function (Column) {                 //遍历表格的
            if (Row > 0 && Row < 10) {
                // console.info(Row + "," + Column);          //打印(行,列)
                let CellData = $(this).text();
                if (CellData.search('★') != -1) {
                    //console.info(Row + "," + Column);          //打印(行,列)
                    //console.info(CellData);
                    CourseParser(CellData, Row, Column);
                }
            }
        });
    });

    if (CourseResult.length == 0) {

        let sections = [];
        sections.push({ section: 1 }, { section: 2 });
        let course = {
            name: "没有获取到课表",
            position: "课表为空",
            teacher: "反馈QQ: 2591081434",
            weeks: weekStr2IntList("1-12"),
            day: 1,
            sections: sections,
        }
        CourseResult.push(course)
    }
    console.info(CourseResult);
    return CourseResult;
}
